package com.guli.mall.pms.controller;

import java.util.Arrays;
import java.util.Map;

import com.guli.common.valid.AddGroup;
import com.guli.mall.pms.feign.ProductCategoryFeignClient;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import com.guli.mall.pms.entity.CategoryBrandRelationEntity;
import com.guli.mall.pms.service.CategoryBrandRelationService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author 罗俊华
 * @email 1007052116@qq.com
 * @date 2020-09-17 11:37:22
 */
@RestController
@RequestMapping("pms/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Resource
    ProductCategoryFeignClient productCategoryFeignClient;

    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取categoryBrandRelation分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size) {
        PageUtils page = categoryBrandRelationService.queryPage(currentPage, size);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{id}")
    @ApiOperation("根据id获取CategoryBrandRelation")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    @PostMapping("/save")
    @ApiOperation("插入CategoryBrandRelation")
    public R save(@RequestBody @Validated(AddGroup.class) CategoryBrandRelationEntity categoryBrandRelation) {

        String categoryName = productCategoryFeignClient.getCategoryName(categoryBrandRelation.getCatelogId());

        categoryBrandRelation.setCatelogName(categoryName);

        categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据id更新CategoryBrandRelation")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {

        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据id批量删除CategoryBrandRelation")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    @GetMapping("/getCategoryBrandRelationByBrandId/{brandId}/{currentPage}/{size}")
    @ApiOperation("通过BrandId获得商品分类与品牌之间的关系")
    public R getCategoryBrandRelationByBrandId(@PathVariable("brandId") Long brandId,
                                               @PathVariable("currentPage") Integer currentPage,
                                               @PathVariable("size") Integer size) {


        PageUtils<CategoryBrandRelationEntity> page = categoryBrandRelationService.getCategoryBrandRelationByBrandId(brandId,currentPage,size);

        return R.ok().put("page", page);
    }

    @ApiOperation("根据catelogId更新categoryName")
    @PostMapping("/updateCategoryName/{catelogId}")
    public R updateCategoryName(@PathVariable("catelogId") Long catelogId, @RequestBody String categoryName){

        categoryBrandRelationService.updateCategoryName(catelogId,categoryName);

        return R.ok();
    }

}
